<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/6/22 0022
 * Time: 10:33
 */

namespace XtwsBack\Controller;


use Think\Controller;

class HelppoorController extends Controller
{
    public function index(){
        $member = M('member');
        $helppoor=M('helppoor');
        $count = $helppoor->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,100);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出

        $helppoor_arr =$helppoor->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($helppoor_arr as $k => $v){
            switch ($v['state']){
                case 0:
                    $helppoor_arr[$k]['state'] = '未开始';
                    break;
                case 1:
                    $helppoor_arr[$k]['state'] = '已开始';
                    break;
                case 2:
                    $helppoor_arr[$k]['state'] = '已结束';
                    break;
                case 3:
                    $helppoor_arr[$k]['state'] = '已删除';
                    break;
            }
        }
        $this->assign('list',$helppoor_arr);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }//end fun

    public function Addgroup(){
        $helppoor = M('helppoor');
        //添加拼团订单到数据库
        $post_group = I("post.");
        if(IS_POST){
            $post_group['starttime'] = strtotime($post_group['starttime']);
            $post_group['endtime'] = strtotime($post_group['endtime']);
            $post_group['addtime'] = time();
            $addhelppoor = $helppoor->add($post_group);
            if ($addhelppoor){
                $this->success('添加成功','index.html');
                return;
            }else{
                $this->error('添加失败');
                return;
            }
        }
        $this->display();
    }//end fun Add

    public function Savegroup(){
        $helppoor = M('helppoor');
        $id = $_GET['id']?$_GET['id']:I('id');
        if($id){
            $helppoor_info = $helppoor->where('id='.$id)->find();
            $member_arr = explode(',',$helppoor_info['member_ranking']);//获取用户id
            if (!in_array('',$member_arr)){
                $member_ranking = array();
                foreach($member_arr as $mk => $mv){
                    $member_info=M("member")->where("id = {$mv}")->field('avatar,nickname,c_id')->find();//获取用户信息
                    $member_info['c_id']=M("shop_community")->where("id = {$member_info['c_id']}")->getField('title');//获取地址
                    $member_info['eva_num'] = M("foretaste_ranking")->where("u_id = {$mv} AND h_id = {$id}")->getField('eva_num');//获取爱心值
                    $member_ranking[]=$member_info;
                }//end foreach
                $helppoor_info['member_ranking']=$member_ranking;
            }//end if
            $helppoor_info['starttime'] = date('Y-m-d H:i:s',$helppoor_info['starttime']);
            $helppoor_info['endtime'] = date('Y-m-d H:i:s',$helppoor_info['endtime']);

//            dump($helppoor_info);
            $post_group = $_POST;
            if (IS_POST){
                $post_group['starttime'] = strtotime($post_group['starttime']);
                $post_group['endtime'] = strtotime($post_group['endtime']);
                foreach($post_group as $pk => $pv){
                    if ($pv == '' || $pv == $helppoor_info[$pk]){
                        unset($post_group[$pk]);
                    }
                }//end foreach
                if (!empty($post_group)){
                    $addgroup = $helppoor->where('id ='.$id)->save($post_group);
                    if ($addgroup){
                        $this->success('添加成功','index.html');
                        return;
                    }else{
                        $this->error('添加失败');
                    }//end if
                }else{
                    $this->error('无修改','index.html');
                }//end if

            }//end if
            $this->assign($helppoor_info);
            $this->display();
        }else{
            $this->error('参数错误');
        }//end if
    }//end fun Save

    public function Deletegroup(){
        $goods = M('foretaste_goods');
        $id = I('id');
        if ($id){
            $delegroup = $goods->where('id='.$id)->save(['state' => 3]);
            if ($delegroup){
                $this->success('删除成功','index.html');
                return;
            }else{
                $this->error('删除失败');
            }
        }
    }//end fun Delete
}//end class